package com.hgsf.service.impl;

import com.hgsf.entity.*;
import com.hgsf.mapper.AdminMapper;
import com.hgsf.service.AdminService;
import com.hgsf.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.springframework.cache.annotation.Cacheable;


@Service
public class AdminServiceImpl implements AdminService {

	AdminMapper adminMapper;


	public AdminServiceImpl(AdminMapper adminMapper) {
		this.adminMapper = adminMapper;
	}

	@Override
	public Admin login(Admin admin) {
		// TODO Auto-generated method stub
		return adminMapper.querySingleAdmin(admin);
	}

	@Override
	public boolean register(Admin admin) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addCourse(Course course) {
		return  adminMapper.insertCourse(course)==1;
	}

	@Cacheable("queryCourse")
	@Override
	public List<Course> queryCourse() {
		List<Course> t = adminMapper.queryAllCourse();
		System.out.println(t);
		return t;
	}

	@Override
	public List<Vector> queryCourseToVector() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Teacher> queryTeacher() {
		return adminMapper.queryAllTeacher();
	}

	@Override
	public Teacher queryTeacherByTname(String tname) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Major> queryMajor() {
		return adminMapper.queryMajor();
	}

	@Override
	@CacheEvict(value={"queryCourse"},allEntries = true)
	public boolean alterCourse(Course course) {
		return adminMapper.updateCourse(course)>0;
	}

	@Override
	public List<Course> queryCourseByCondition(String condition) {
		// TODO Auto-generated method stub
		return adminMapper.queryCourseByCondition(condition);
	}

	@Override
	public boolean delectCourse(int cid) {
		return adminMapper.delectCourse(cid)>0;
	}

	@Override
	public boolean addStudent(Student s) {
		return adminMapper.insertStudent(s)==1;
	}

	@Override
	public boolean addTeacherMsg(Teacher t) {
		return adminMapper.insertTeacherMsg(t)==1;
	}

	@Override
	public List<College> queryCollege() {
		return adminMapper.queryCollege();
	}

	@Override
	public boolean addMajor(Major m) {
		return adminMapper.insertMajor(m)==1;
	}

	@Override
	public List<Map<String,String>> queryStuSelectedCourse(String condition) {
		// TODO Auto-generated method stub
		return adminMapper.queryStuSelectedCourse(condition);
	}

	@Override
	public List<Course> queryAllCourse(long sid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean alterSelectedCourse(long sid, int oldCid, int newCid) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Student> queryStudentByCodition(String condition) {

		return adminMapper.queryStudentByCondition(condition);
	}

	@Override
	public List<Vector> queryStudentByCoditionToVectoer(List<Map<String, String>> list) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean alterStudent(Student student) {
		return adminMapper.alterStudent(student)==1;
	}

	@Override
	public boolean deleteStudent(long sid) {

		return adminMapper.deleteStudent(sid)==1;
	}

	@Override
	public List<Map<String, String>> queryTeacherByCondition(String condition) {
		return adminMapper.queryTeacher(condition);
	}

	@Override
	public List<Vector> queryTeacherByConditionToVector(List<Map<String, String>> list) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deleteTeacher(int tid) {
		// TODO Auto-generated method stub
		return adminMapper.deleteTeacher(tid)>0;
	}

	@Override
	public boolean alterTeacher(Map<String, String> map) {
		boolean b = false;
		try {
			if(adminMapper.alterTeacher(map)==1){
                b=true;
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}

	@Override
	public boolean resetAdminPsw(int aid) {
		return adminMapper.resetAdminPsw(aid)==1;
	}

	@Override
	public boolean resetTeacherPsw(int tid) {
		return adminMapper.resetTeacherPsw(tid)==1;
	}

	@Override
	public boolean resetStudentPsw(long sid) {
		return adminMapper.resetStudentPsw(sid)==1;
	}

	@Override
	public List<Map<String, String>> queryCourseByPage(Page<Map<String, String>> page) {
		return adminMapper.queryCourseByPage(page);
	}

	@Override
	public int queryTotalCourseCount() {
		return adminMapper.queryTotalCourseCount();
	}

	@Override
	public boolean deleteSelectedCourse(List<SelectedCourse> selectedCourses) {
		for(SelectedCourse sc:selectedCourses){
			if(adminMapper.deleteSelectedCourse(sc)!=1){
				return false;
			}
		}
		return true;
	}
}
